from src.Utils.index import colored_text
from flask import g

class PersonDao:
    
    def __init__(self):
        pass
    
    def register(self,phone: str):
        """
        根据手机号注册新用户
        
        参数
            phone: 手机号
        """
        db = g.db
        cursor = db.cursor() if db is not None else None
        sql = 'INSERT INTO person (phone) VALUES (%s)'
        print(colored_text(sql,'yellow'))
        try:
            cursor.execute(sql,(phone,))
            new_id = cursor.lastrowid
            db.commit()
            cursor.close()
            return new_id
        except:
            print(colored_text("-----注册失败-----","red"))
            if db is not None:
                db.rollback()
            return None
        finally:
            if cursor is not None:
                cursor.close()
                
    def login(self,phone: str):
        """
        根据手机号登录
        
        参数
            phone: 手机号
        """
        db = g.db
        cursor = db.cursor() if db is not None else None
        find_user_sql = "SELECT id FROM person WHERE phone = %s"
        try:
            cursor.execute(find_user_sql,(phone,))
            result = cursor.fetchone()
            cursor.close()
            return result.get('id')    
        except:
            print(colored_text("------查询失败-----","red"))
            return None
        finally:
            if cursor is not None:
                cursor.close()
                
    def getOrder(self,userId):
        """
        查询用户购物车
        
        参数
            userId: 用户id
        """
        db = g.db
        cursor = db.cursor() if db is not None else None
        sql = f"SELECT info FROM orderinfo WHERE userId = {userId}"
        try:
            cursor.execute(sql)
            result = cursor.fetchone()
            cursor.close()
            return result.get("info")
        except:
            print(colored_text("------查询失败-----","red"))
            return None
        finally:
            if cursor is not None:
                cursor.close()
                
    def updateOrder(self,userId,info):
        """
        更新用户购物车
        
        参数:
            userId: 用户id
            info: 用户购物车
            
        返回:
            id: 购物车id
        """
        db = g.db
        cursor = db.cursor() if db is not None else None
        sql =f"DELETE FROM orderinfo WHERE userId = {userId}" if info == '{}' else f"UPDATE orderinfo SET info = '{info}' WHERE userId = {userId}"
        try:
            _sql = f"SELECT * FROM orderinfo WHERE userId = {userId}"
            cursor.execute(_sql)
            res = cursor.fetchone()
            if not res:
                sql = f"INSERT INTO orderinfo VALUES (NULL,'{info}',{userId})"
            cursor.execute(sql)
            db.commit()
            cursor.close()
            return True
        except:
            print(colored_text("------查询失败-----","red"))
            if db is not None:
                db.rollback()
            return None
        finally:
            if cursor is not None:
                cursor.close()